package com.ihrm.system.service;

import com.ihrm.common.service.BaseService;
import com.ihrm.common.utils.IdWorker;
import com.ihrm.common.utils.PermissionConstants;
import com.ihrm.domain.system.Permission;
import com.ihrm.domain.system.Role;
import com.ihrm.system.dao.PermissionDao;
import com.ihrm.system.dao.RoleDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class RoleService extends BaseService<Role> {

    @Autowired
    IdWorker idWorker;

    @Autowired
    RoleDao roleDao;

    @Autowired
    PermissionDao permissionDao;

    /**
     * 添加角色
     */
    public void save(Role role) {
        //填充其他参数
        role.setId(idWorker.nextId() + "");
        roleDao.save(role);
    }

    /**
     * 更新角色
     */
    public void update(Role role) {
        Role targer = roleDao.getOne(role.getId());
        targer.setDescription(role.getDescription());
        targer.setName(role.getName());
        roleDao.save(targer);
    }

    /**
     * 根据ID查询角色
     */
    public Role findById(String id) {
        return roleDao.findById(id).get();
    }

    /**
     * 删除角色
     */
    public void delete(String id) {
        roleDao.deleteById(id);
    }

    /**
     * 获取所有角色
     * @return
     */
    public List<Role> findAll(String companyId){
        return roleDao.findAll(getSpecification(companyId));
    }

    /**
     * 分页获取公司权限
     * @param companyId
     * @param page
     * @param size
     * @return
     */
    public Page<Role> findSearch(String companyId, int page, int size){
        Specification<Role> specification = new Specification<Role>() {
            @Override
            public Predicate toPredicate(Root<Role> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                return criteriaBuilder.equal(root.get("companyId").as(String.class), companyId);
            }
        };
        return roleDao.findAll(specification, PageRequest.of(page -1, size));
    }

    /**
     * 编辑角色权限
     * @param id
     * @param permIds
     */
    public void assignPrem(String id, List<String> permIds){
        Role role = findById(id);
        Set<Permission> permissions = new HashSet<>();
        for(int i = 0; permIds != null && i < permIds.size(); i++){
            Permission permission = permissionDao.findById(permIds.get(i)).get();
            //需要根据父id和类型查询API权限列表
            List<Permission> apiPermission = permissionDao.findByTypeAndPid(PermissionConstants.PY_API, permission.getId());
            permissions.addAll(apiPermission);
            permissions.add(permission);
        }
        role.setPermissions(permissions);
        roleDao.save(role);
    }

}
